app.controller('memberCardAddCtrl',['$scope','memberCardAddService','$state','toaster','$stateParams',
    function($scope,memberCardAddService,$state,toaster,$stateParams){
    $scope.memberCard = {
        isFreeCarriage:0,
        isDiscount:0,
        isPresentCoupon:0,
        isOpenConsumePrice:0,
        isOpenConsumeIntegral:0,
        isOpenBuyCount:0,
        validTimeState:0,
        activationState:0,
        isPhoneVerification:0,
        isPerfectMessage:0,
        buyCount:0,
        consumePrice:0,
        consumeIntegral:0,
        type:$stateParams.type,
        cardGrade:0
    };

    $scope.addCards=function(){
        memberCardAddService.addMemberCard($scope).then(function(result){
        switch (result.status){
            case 200:
                toaster.pop('success', "提示", "操作成功");
                $state.go('app.memberCard');
                break;
            case 624:
                toaster.pop('waiting', "提示", result.message);
                $state.go('app.404');
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break;
            }
        })
    };

    //获取该商家优惠券
    $scope.coupons={};
    $scope.query = function() {
        memberCardAddService.findCoupons($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.couponsList = result.data;
                    break;
                case 624:
                    toaster.pop('error', "操作失败", "找不到该店铺,请刷新重试");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.query();

    //获取该商家有规则会员卡的最大值
    $scope.maxCard={};
    $scope.queryMaxCard = function() {
        memberCardAddService.queryMaxCard($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.maxCard = result.data;
                    $scope.memberCard.buyCount=$scope.maxCard.buyCount+1;
                    $scope.memberCard.consumePrice=$scope.maxCard.consumePrice+0.01;
                    $scope.memberCard.consumeIntegral=$scope.maxCard.consumeIntegral+1;
                    $scope.memberCard.cardGrade=$scope.maxCard.cardGrade+1;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.queryMaxCard();
}]);
